#!/usr/bin/env python
# coding: utf-8

# In[45]:


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import gridspec


# In[2]:


boy=pd.read_excel('C:/Users/Administrator/Desktop/模块五/Matplotlib数据可视化/Matplotlib数据可视化/matplotlib作业/matplotlib作业/体测分数_男生.xls')
girl=pd.read_excel('C:/Users/Administrator/Desktop/模块五/Matplotlib数据可视化/Matplotlib数据可视化/matplotlib作业/matplotlib作业/体测分数_女生.xls')


# In[51]:


#第一题
pao=pd.cut(boy['男1000米跑'].values,bins=3,labels=['slow','normal','fast'],right=True)
yin=pd.cut(boy['男引体'].values,bins=3,labels=['weak','normal','strong'],right=True)
data_pao=pao.value_counts()
data_yin=yin.value_counts()

plt.figure(figsize=(9,6))
gs=gridspec.GridSpec(1,2)
plt.subplot(gs[0,0])
_=plt.pie(data_pao,labels=['slow','normal','fast'],autopct='%0.2f%%')
plt.title('男子1000米跑',fontfamily='FangSong')

plt.subplot(gs[0,1])
_=plt.pie(data_yin,labels=['weak','normal','strong'],autopct='%0.2f%%')
plt.title('男子引体向上',fontfamily='FangSong')


# In[75]:


#第二题

plt.figure(figsize=(9,6))
gs=gridspec.GridSpec(1,2)
plt.subplot(gs[0,0])
cond=girl['女800米跑']<6
plt.hist(girl[cond]['女800米跑'],bins=4)
plt.title('女800米跑',fontfamily='FangSong')
plt.grid(axis='y',alpha=0.75)

plt.subplot(gs[0,1])
plt.hist(girl['女跳远'],bins=4)
_=plt.title('女跳远',fontfamily='FangSong')
plt.grid(axis='y',alpha=0.75)


# In[96]:


#第三题
def boy_weight(x):
    if x<=16.4:
        return "低体重"
    elif 16.5<=x<=23.2:
        return "正常"
    elif 23.3<=x<=26.3:
        return "超重"
    elif 26.4<=x:
        return "肥胖"

def girl_weight(x):
    if x<=16.4:
        return "低体重"
    elif 16.5<=x<=22.7:
        return "正常"
    elif 22.8<=x<=25.2:
        return "超重"
    elif 25.3<=x:
        return "肥胖"
    
boy_w=boy['BMI'].round(1).map(lambda x : boy_weight(x))
girl_w=girl['BMI'].round(1).map(lambda x : girl_weight(x))
boy_cond=boy_w.value_counts()
girl_cond=girl_w.value_counts()
plt.figure(figsize=(16,9))
_=plt.pie(boy_cond,autopct='%0.2f%%',colors=['green','blue','red','yellow'],labeldistance=1.1,radius=1,pctdistance=0.85,wedgeprops={'linewidth':5,'width':0.3,'edgecolor':'white'})
_=plt.pie(girl_cond,autopct='%0.2f%%',radius=0.7,pctdistance=0.7,colors=['green','blue','red','yellow'],wedgeprops={'linewidth':5,'width':0.5,'edgecolor':'white'})
_=plt.title('男女体重指数占比',fontfamily='FangSong')
plt.legend(['normal','overweight','fat','underweight'])

